﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.App.DTO;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.App.Applications.Photos
{
    public class PhotosQueryCommandHandler : IRequestHandler<PhotosQueryCommand, APIResult<List<PhotosDto>>>
    {
        private readonly IBaseRepository<T_Photos> photosRepository;

        public PhotosQueryCommandHandler(IBaseRepository<T_Photos> photosRepository)
        {
            this.photosRepository = photosRepository;
        }

        public Task<APIResult<List<PhotosDto>>> Handle(PhotosQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<PhotosDto>> result = new APIResult<List<PhotosDto>>();
            result.Code = APIEnum.ok;
            result.Message = "获取成功";
            List<PhotosDto> dtos = new List<PhotosDto>();
            var list = photosRepository.GetAll().AsNoTracking().Where(x => x.Batch == request.Batch&&x.InformationType==request.InformationType&&x.PhotoType==request.PhotoType);
            foreach (var item in list)
            {
                PhotosDto dto = new PhotosDto 
                {
                    Name=item.PhotoType,
                    Url=item.ImageUrl
                };
                dtos.Add(dto);
            }

            result.Data = dtos;
            return Task.FromResult(result);
        }
    }
}
